package com.dajiba.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class ImageFmUserLikePortrait extends ViewGroup {

    ArrayList<String> datas = new ArrayList<>();

    public ImageFmUserLikePortrait(Context context) {
        super(context);
        init();
    }

    public ImageFmUserLikePortrait(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ImageFmUserLikePortrait(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        for (int i = 0; i < 4; i++) {
            datas.add("");
            ImageView imageView = new ImageView(getContext());
            addView(imageView);
            imageView.setBackgroundColor(Color.BLACK);

        }
        setBackgroundColor(Color.BLUE);
    }

    //子view宽高
    int mChildViewWidth = SizeUtils.dp2px(30);
    int mChildViewHeight = mChildViewWidth;

    //上下间隔
    int mVerticalInterval = SizeUtils.dp2px(5);
    int mHorizontalInterval = SizeUtils.dp2px(5);

    //横向个数
    int mHorizontalSum = 3;

    LayoutParams mChildLayoutParams = new LayoutParams(mChildViewWidth, mChildViewWidth);

    public void setData(ArrayList<String> strings) {
        removeAllViews();
        datas.clear();
        for (int i = 0; i < strings.size(); i++) {
            if (i > 50) break;
            datas.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1452799892,2751917670&fm=26&gp=0.jpg");
            ImageView imageView = new ImageView(getContext());
            imageView.setBackgroundColor(Color.BLACK);
            addView(imageView);
        }
        requestLayout();
    }


    private ImageClickListener mImageClickListener;

    public void setImageClickListener(ImageClickListener imageClickListener) {
        mImageClickListener = imageClickListener;
    }

    public interface ImageClickListener {
        void onClick(int position);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        int specWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int specWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int specHeightSize = MeasureSpec.getSize(heightMeasureSpec);
        int specHeightMode = MeasureSpec.getMode(heightMeasureSpec);
        setMeasuredDimension(specWidthSize, mChildViewWidth);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int usedHorizontal = 0;//水平已经使用的距离
        int usedVertical = 0;//垂直已经使用的距离
        int width = getMeasuredWidth();
        LogUtils.d("onlayout",width,getChildCount(),mChildViewWidth);
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            mChildLayoutParams.width = mChildViewWidth;
            mChildLayoutParams.height = mChildViewWidth;
            view.setLayoutParams(mChildLayoutParams);
            //判断是否要换行
            if (mChildViewWidth + usedHorizontal > width) {
                //要换行了说明摆满了
                LogUtils.d("要换行了说明摆满了",width,getChildCount(),mChildViewWidth);

                view.setVisibility(View.GONE);
                break;
            }
            LogUtils.d("摆");
            view.layout(usedHorizontal, usedVertical, usedHorizontal + mChildViewWidth, usedVertical + mChildViewWidth);
            usedHorizontal = usedHorizontal + mHorizontalInterval + mChildViewWidth;
//            ImageHelper.Companion.loadRCImage(
//                    getContext(), datas.get(i),
//                    ImageHelper.Companion.getRADIUS_VALUE(),
//                    true
//            ).into((ImageView) view);

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}
